package com.example.withroadlogistics.MyRule;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.withroadlogistics.Adapter.Adapter_RuleNo;
import com.example.withroadlogistics.R;
import com.example.withroadlogistics.model.TRule;
import com.example.withroadlogistics.until.HttpCallbackListener;
import com.example.withroadlogistics.until.HttpUtil;
import com.example.withroadlogistics.until.MySharedPreferences;
import com.example.withroadlogistics.until.constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 作者：Administrator on 2020/2/16 0016 17:02
 * 邮箱：1415862429@qq.com
 */
public class RuleNo extends AppCompatActivity {
    private ListView mListView;
    private List<TRule> mList = new ArrayList<>();

    private String TAG = "ifu25";
    private String originAddress = constant.url+ "rule/getRuleNoByUserId";

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = "";

            if ("OK".equals(msg.obj.toString())){
                result = "success";
            }else if ("Wrong".equals(msg.obj.toString())){
                result = "fail";
            }else {
                String data = msg.obj.toString();
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    String code = jsonObject.getString("code");
                    if (code.equals("0")) {
                        result = jsonObject.getString("data");
                        // Toast.makeText(Mytransport.this, result, Toast.LENGTH_LONG).show();
                        Toast.makeText(RuleNo.this, "获取相关内容成功！", Toast.LENGTH_LONG).show();
                        try {
                            JSONArray jsonList = jsonObject.getJSONArray("data");
                            for(int i=0;i<jsonList.length();i++){
                                Log.i(TAG, "handleMessage: "+jsonList);
                                JSONObject json = jsonList.getJSONObject(i);

                                int ruleId=Integer.parseInt(json.getString("ruleId"));
                                String ruContent=json.getString("ruContent");
                                String ruTime=json.getString("ruTime");
                                String registerTime=json.getString("registerTime");
                                String carId=json.getString("carId");
                                String userId=json.getString("userId");
                                String taskId=json.getString("taskId");
                                String ruRemark=json.getString("ruRemark");
                                mList.add(new TRule(ruleId,ruContent,ruTime,registerTime,carId,userId,taskId,ruRemark));
                                Adapter_RuleNo adapter = new Adapter_RuleNo(RuleNo.this,mList);
                                //MySharedPreferences.setRouteId(String.valueOf(routeId), Mytransport.this);
                                mListView.setAdapter(adapter);
                                // finish();一闪而过
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(RuleNo.this, "网路异常，请退出登陆重试！", Toast.LENGTH_LONG).show();
                        }


                    }else {
                        result = jsonObject.getString("data");
                        // Toast.makeText(Mytransport.this, result, Toast.LENGTH_LONG).show();
                        Toast.makeText(RuleNo.this, "获取相关内容失败！", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(RuleNo.this, "获取相关内容异常！", Toast.LENGTH_LONG).show();
                }

            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rule);
        mListView =findViewById(R.id.mListView);
        gethistory();
    }

    private boolean isInputValid() {
        //检查用户输入的合法性，这里暂且默认用户输入合法
        return true;
    }

    public void gethistory() {
        //取得用户输入的账号和密码
        if (!isInputValid()){
            return;
        }
        String userId = (String) MySharedPreferences.getUserId(RuleNo.this);
        if (userId.isEmpty()) {
            Toast.makeText(RuleNo.this, "用户失效请重新登陆！", Toast.LENGTH_LONG).show();
            return;
        }
        HashMap<String, String> params = new HashMap<String, String>();
        params.put(TRule.USERID, userId);
        try {
            String compeletedURL = HttpUtil.getURLWithParams(originAddress, params);
            HttpUtil.sendHttpRequest(compeletedURL, new HttpCallbackListener() {
                @Override
                public void onFinish(String response) {
                    Message message = new Message();
                    message.obj = response;
                    mHandler.sendMessage(message);
                }

                @Override
                public void onError(Exception e) {
                    Message message = new Message();
                    message.obj = e.toString();
                    mHandler.sendMessage(message);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
